package util;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class Dbconnect{
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static Statement st;
	private static Connection c;
	/**
	 * 使用静态代码快读取配置文件 完成驱动的加载
	 */
	static {
		// 定义一个Map集合--Properties
		Properties properties = new Properties();
		try {
			// 使用Properties集合的load方法加载配置文件
			properties.load(Dbconnect.class.getClassLoader().getResourceAsStream("db.properties"));
			driver = properties.getProperty("jdbc.driver");
			url = properties.getProperty("jdbc.url");
			user = properties.getProperty("jdbc.user");
			password = properties.getProperty("jdbc.password");
			System.out.println(driver);
			// 加载驱动
			
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//数据库获取连接
	public static Connection getconnect() throws SQLException {
		Connection connection = DriverManager.getConnection(url, user, password);
		c=connection;
		return connection;
	}
	
	//实例化
	public static Statement createstatement(Connection c) throws SQLException {
		st = c.createStatement();
		return c.createStatement();
	}
	
	//实例化
	public static Statement createstatement() throws SQLException {
		return createstatement(c);
	}
	
	//数据库更新
	public static int update(Statement st, String sql) throws SQLException {
		return st.executeUpdate(sql);
	}
	
	//数据库更新
	public static int update(String sql) throws SQLException {
		return update(st,sql);
	}
	public static void init() throws SQLException {
		getconnect();
		createstatement();
	}
	public static <T> List<T> query(Statement st, String sql,Class<T> cs) throws Exception {
		System.out.println(sql);
		Field[] fd = cs.getDeclaredFields();
		List<T> list = new ArrayList<T>();
		ResultSet set = st.executeQuery(sql);
		while (set.next()) {
			T t = cs.getDeclaredConstructor().newInstance();
			for(Field f:fd) {
				f.setAccessible(true);	
				if(f.getType().equals(String.class)||f.getType().equals(Integer.class)||f.getType().equals(Double.class)) {
					f.set(t,set.getObject(f.getName()));
				}
		
			}
			list.add(t);
		}
		return list;
	}

	public static <T> List<T> query (String sql,Class<T> cs) throws Exception{
		return query(st,sql,cs);
	}
	
	
	public static <T> T queryone(Statement st, String sql,Class<T> cs) throws Exception {
		System.out.println(sql);
		Field[] fd = cs.getDeclaredFields();
		List<T> list = new ArrayList<T>();
		ResultSet set = st.executeQuery(sql);
		while (set.next()) {
			T t = cs.getDeclaredConstructor().newInstance();
			for(Field f:fd) {
				f.setAccessible(true);	
				if(f.getType().equals(String.class)||f.getType().equals(Integer.class)||f.getType().equals(Double.class)) {
					f.set(t,set.getObject(f.getName()));
				}
		
			}
			list.add(t);
		}
		T t = null;
		
		if(list.size()>0) {
			t = list.get(0);
		}
		
		return t;
	}

	public static <T> T queryone(String sql,Class<T> cs) throws Exception{
		return queryone(st,sql,cs);
	}
	
	
	
	public static void close(Connection c,Statement st) throws SQLException {
		if(c!=null) {
			c.close();
		}
		if(st!=null) {
			st.close();
		}
	}
	public static void close() throws SQLException {
		if(c!=null) {
			c.close();
		}
		if(st!=null) {
			st.close();
		}
	}
	public static void main(String[] args) {
		System.out.println("??");
	}
}
